package com.jm.member.common.messages.service;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.jm.member.common.messages.config.AliMessagesConfig;
import com.jm.member.common.messages.config.WeChartConfig;
import com.jm.member.common.messages.entity.AliResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

@Configuration
@ImportAutoConfiguration(AliMessagesConfig.class)
@ConditionalOnProperty(name = "alimessages.enabled",havingValue = "true")
public class AliMessageService {

       @Autowired
       AliMessagesConfig messagesConfig;


      public AliResponse sendSms(String mobile, String signName, String templateCode){
        DefaultProfile profile = DefaultProfile.getProfile(messagesConfig.getRegionId(), messagesConfig.getAccessKeyId(),
                messagesConfig.getSecret());
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(messagesConfig.getDomain());
        request.setSysVersion(DateUtil.format(messagesConfig.getVersion(),"yyyy-MM-dd"));
        request.setSysAction(messagesConfig.getAction());
        request.putQueryParameter("RegionId", messagesConfig.getRegionId());
        request.putQueryParameter("PhoneNumbers", mobile);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);
        int code = RandomUtil.randomInt(100000, 999999);
        Map map=new HashMap();
        map.put("code",code);
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map));
      try {
            CommonResponse response = client.getCommonResponse(request);
            JSONObject parse = (JSONObject) JSONObject.parse(response.getData());
            AliResponse reponse=new AliResponse(
                    parse.getString("Message"),
                    parse.getString("RequestId"),
                    parse.getString("BizId"),
                    parse.getString("Code"),
                    String.valueOf(code));
            return reponse;
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    };

    public Integer getTime(){
        return messagesConfig.getTime();
    }

    public AliMessagesConfig getMessagesConfig() {
        return messagesConfig;
    }

}
